﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace ThanhTu.GNC.Media.KQXS.Common
{
    public static class Utils
    {
        public static string MD5Encrypt(string input)
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] outputBytes = md5.ComputeHash(inputBytes);
            md5 = null;
            return (BitConverter.ToString(outputBytes).Replace("-", string.Empty));
        }

        public static TripleDES CreateTripleDES(string key)
        {
            if (key.Length > 24) { key.Substring(0, 24); }
            TripleDES des = new TripleDESCryptoServiceProvider();
            des.Key = Encoding.ASCII.GetBytes(key);
            des.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
            return des;
        }

        public static string EncryptTripleDES(string cipherText, string key)
        {
            byte[] b = Encoding.ASCII.GetBytes(cipherText);
            TripleDES des = CreateTripleDES(key);
            ICryptoTransform ct = des.CreateEncryptor();
            byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
            b = null;
            des = null;
            ct = null;
            return Convert.ToBase64String(output);
        }

        public static string DecryptTripleDES(string cipherText, string key)
        {
            byte[] b = Convert.FromBase64String(cipherText);
            TripleDES des = CreateTripleDES(key);
            ICryptoTransform ct = des.CreateDecryptor();
            byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
            des = null;
            ct = null;
            return Encoding.ASCII.GetString(output);
        }

        public static byte[] ToOracleBLOB(string input)
        { return Encoding.UTF8.GetBytes(input); }

        public static string ToUTF8String(byte[] input)
        { return Encoding.UTF8.GetString(input); }

        private static readonly string[] VietnameseSigns = new string[] {    
        "aAeEoOuUiIdDyY",
        "áàạảãâấầậẩẫăắằặẳẵ",
        "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
        "éèẹẻẽêếềệểễ",
        "ÉÈẸẺẼÊẾỀỆỂỄ",
        "óòọỏõôốồộổỗơớờợởỡ",
        "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
        "úùụủũưứừựửữ",
        "ÚÙỤỦŨƯỨỪỰỬỮ",
        "íìịỉĩ",
        "ÍÌỊỈĨ",
        "đ",
        "Đ",
        "ýỳỵỷỹ",
        "ÝỲỴỶỸ"
        };


        public static string RemoveSign4VietnameseString(string str)
        {
            for (int i = 1; i < VietnameseSigns.Length; i++)
            {
                for (int j = 0; j < VietnameseSigns[i].Length; j++)
                { str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]); }
            }
            return str;
        }

        public static DateTime ToDateTime(string input)
        {
            string temp = string.Empty;

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] - 48 >= 0 &&
                    input[i] - 48 <= 9)
                { temp += input[i]; }
            }

            if (temp.Length < 8)
            { return DateTime.Now; }
            else
            {
                temp = temp.Substring(0, 8);
                return new DateTime(Convert.ToInt32(temp.Substring(4, 4)), Convert.ToInt32(temp.Substring(2, 2)), Convert.ToInt32(temp.Substring(0, 2)));
            }
        }

        public static bool IsNumeric(string input)
        {
            int output = 0;
            return int.TryParse(input, out output);
        }
        public static string ToNullSet0(string input)
        {
            if (Convert.ToInt32(input) == 0)
            {
                return null;
            }
            return input;
        }
    }
}
